<?php
namespace zhtp;

/**
 * 特定常量工具类
 */
class ConstUtil
{
    // 示例特定常量数据-定义在其它类const里
    // public const TYPE = [
    //     'KEY1' => [1, '描述1'],
    //     'KEY2' => [2, '描述2'],
    // ];


    /**
     * 获取常量数据-通过值
     * 
     * 传(Class::TYPE,1) ,返回 ['KEY1','描述1']
     * (1-来自db/用户输入)
     */
    public static function getByValue($const_data, $data_val)
    {
        foreach ($const_data as $key => $value) {
            if ($value[0] === $data_val) {
                unset($value[0]);
                return [$key, ...$value];
            }
        }
        return null;
    }

    /**
     * key和value互换 
     * 
     * 传(Class::TYPE),返回['1' => ['KEY1', '描述1']]
     */
    public static function exchangeKeyAndValue($const_data)
    {
        $new_data = [];
        foreach ($const_data as $key => $value) {
            $newkey = $value[0];
            unset($value[0]);
            $new_data[$newkey] = [$key, ...$value];
        }
        return $new_data;
    }

    /**
     * 获取所有的value值
     * 
     * 传(Class::TYPE,true),返回'1,2'
     * 传(Class::TYPE,false),返回[1,2]
     */
    public static function getAllValues($const_data, $implode = true)
    {
        $values = [];
        foreach ($const_data as $key => $value) {
            $values[] = $value[0];
        }
        if ($implode) {
            $values = implode(',', $values);
        }
        return $values;
    }
}
